<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\Response;

use App\Models\CompanyDevelop;

class CompanyDevelopController extends BaseController
{
    /**
     * Display a listing of the resource.
     *
     * @return Response
     */
    public function index()
    {
        if(!$this->user->can('editCompanyDev')) abort(405);
        
        $develops = CompanyDevelop::paginate(20);
        return view('admin.enterprise.companydevelop.index')->with('develops', $develops);
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return Response
     */
    public function create()
    {
        if(!$this->user->can('viewCompanyDev')) abort(405);
        
        return view('admin.enterprise.companydevelop.add')->with('blademod', 'add');
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  Request  $request
     * @return Response
     */
    public function store(Request $request)
    {
        if(!$this->user->can('editCompanyDev')) abort(405);
        
        //表单验证
        $this->validate($request, [
            'name' => 'required|max:100',
        ]);
        $id = $request->get('id', 0);
        $name = $request->get('name');
        if ($id >0){
            $develop = CompanyDevelop::find($id);
            $develop->name = $name;
            $develop->save();
        }
        else {
            /*查找name是否有重复*/
            CompanyDevelop::firstOrCreate(['name' => $name]);
        }
        return redirect("admin/enterprise/company_develop");
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return Response
     */
    public function show($id)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return Response
     */
    public function edit($id)
    {
        if(!$this->user->can('viewCompanyDev')) abort(405);
        
        $develop = CompanyDevelop::find($id);
        return view('admin.enterprise.companydevelop.add')->with('develop', $develop)->with('blademod', 'edit');
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  Request  $request
     * @param  int  $id
     * @return Response
     */
    public function update(Request $request, $id)
    {
        //
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return Response
     */
    public function destroy($id)
    {
        if(!$this->user->can('deleteCompanyDev')) 
            return $this->formatJson('error',$this->permerror);
        
        $develop = CompanyDevelop::find($id);
        if ($develop->delete())
            return  Response::json(array('status'=>'success','msg'=>'发展阶段 ['. $develop->name .'] 删除成功! '));
        else
            return  Response::json(array('status'=>'error','msg'=>'删除失败'));
    }
}
